/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.telsoft.util;

import com.telsoft.business.DAOHelperBusiness;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;

/**
 *
 * @author rongnk
 */
public class DBUtil {
    public static int getIDFromSequence(String sequence_name) throws SQLException
    {
        ResultSet rs = null;
        Connection connect = null;
        PreparedStatement stmt = null;
        try
        {
            connect = DAOHelperBusiness.getConnection();
            String StrSQL = "SELECT " + sequence_name + ".nextVal FROM DUAL";
            stmt = connect.prepareStatement(StrSQL);
            rs = stmt.executeQuery();
            if(rs.next())
            {
                return Converter.convertStringToInt(rs.getString(1));                
            }
        }
        catch(Exception ex)
        {
            return -1;
        }
        finally
        {
            DAOHelperBusiness.closeConnection(connect);
            DAOHelperBusiness.closeStatement(stmt);
        }
        return -1;
    }
    public enum FUNC_NAME
    {
        ERR, ADD, EDIT, DELETE, INF;
        @Override public String toString() {
        String s = super.toString();
        return s.substring(0, 1) + s.substring(1).toUpperCase();
        }
    }
    public static void log(FUNC_NAME func_name, String strDetail)
    {
        RequestContext context = RequestContext.getCurrentInstance();
        String strFuncName = getMessage("notice");
        
        switch(func_name)
        {       
            case INF:
            {
                break;
            }            
            case ADD:
            {
                strDetail = getMessage("add_sucess");
                break;
            }
            case EDIT:
            {
                strDetail = getMessage("edit_sucess");
                break;
            }
            default:
            {   
                strDetail = getMessage("del_sucess");
                break;
            }
        }
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, strFuncName, strDetail);            
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    public static void logError(FUNC_NAME func_name, String strDetail)
    {
        RequestContext context = RequestContext.getCurrentInstance();
        String strFuncName = getMessage("error");
        
        if(strDetail.indexOf("EMPLOYEE_INFO_UK1") != -1)
        {
            strDetail = getMessage("employee_code_exits");
        }
        if(strDetail.indexOf("FK_EMPOLYEE_EMPLOYEE__EMPLOYEE") != -1)
        {
            strDetail = getMessage("employee_name_not_exist");
        }           
        else if(strDetail.indexOf("FK_EMPLOYEE_EMPLOYEE__APP_ARE") != -1)
        {
            strDetail = getMessage("area_not_exits");
        }    
        else if(strDetail.indexOf("ASSET_EMPLOYEE_FK2") != -1)
        {
            strDetail = getMessage("employee_name_not_exist");
        }          
        else if(strDetail.indexOf("FK_EMPLOYEE_EMPLOYEE__APP_PARA") != -1)
        {
            strDetail = getMessage("param_not_exits");
        }
        else if(strDetail.indexOf("EMP_INFO_UK_PASSPORT") != -1)
        {
            strDetail = getMessage("passport_exist");
        }        
        else if(strDetail.indexOf("number") != -1)
        {
            strDetail = getMessage("invalid_number");
        }        
        
        else
            switch (func_name) 
            {
                case ERR: 
                {
                    break;
                }
                case ADD: 
                {
                    strDetail = getMessage("add_unsucess");
                    break;
                }
                case EDIT: 
                {
                    strDetail = getMessage("edit_unsucess");
                    break;
                }
                default: 
                {
                    strDetail = getMessage("del_unsucess");
                    break;
                }
            }    

        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, strFuncName, strDetail);            
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    
    static public String getMessage(String obj)
    {
        return ResourceBundle.getBundle("com.telsoft.resources.bundle.admin.PP_DBUltil").getObject(obj).toString();
    }
}
